package com.study.leetcode;

import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;

public class Solution_71 {
    public static String simplifyPath(String path) {
        String[] split = path.split("/");
        int temp;
        boolean f;
        for (int i = 0; i < split.length; i++) {
            if (split[i].equals(".")) {
                split[i] = null;
            } else if (split[i].equals("..")) {
                split[i] = null;
                temp = i - 1;
                f = true;
                while (temp >= 0 && f) {
                    if (split[temp] != null) {
                        split[temp] = null;
                        f = false;
                    }
                    temp--;
                }
            } else if ("".equals(split[i])) {
                split[i] = null;
            }
        }
        return "/" + Arrays.stream(split)
                .filter(Objects::nonNull)
                .collect(Collectors.joining("/"));
    }

    public static void main(String[] args) {
        String path = simplifyPath("/home////aa/bbb/../../..//");
        System.out.println("path = " + path);
    }
}
